import 'dart:developer';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
// import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path_provider/path_provider.dart';
import 'lib/SharePage.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //用来保存分享过的图片
  List<PickedFile> _fileList = [];

  //打开相册或者相机
  openFile(int type) async {
    ImagePicker()
        .getImage(source: type == 0 ? ImageSource.gallery : ImageSource.camera,)
        .then((value) async{
          // log("长度${File(value.path).lengthSync()}");
      if (value != null) {
        // Navigator.push(context, MaterialPageRoute(builder: (_)=>NewPage(value)));
        // return;
        Navigator.push(
                context, MaterialPageRoute(builder: (_) => SharePage(value)))
            .then((result) {
          if(result!=null){
            _fileList.add(result);
            setState(() {});
          }
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // drawer: Text("JJO"),
      appBar: AppBar(
        title: Text("Photo Scanner",style: TextStyle(color: Colors.black),),
        elevation: 0,
        backgroundColor: Colors.transparent,
        brightness: Brightness.light,
      ),
      body: Column(
        children: [
          Container(
            child: Row(
              children: [
                Expanded(
                  child: Container(
                    margin: EdgeInsets.all(10),
                    height: 100,
                    // padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Color(0xff334455),
                        borderRadius: BorderRadius.circular(10)),
                    child: IconButton(
                      key: ValueKey("file"),
                      onPressed: () => openFile(0),
                      icon: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.file_copy,
                            size: 40,
                            color: Colors.white,
                          ),
                          SizedBox(height: 5,),
                          Text("From Gallary",style: TextStyle(color: Colors.white),)
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 100,
                    margin: EdgeInsets.all(10),
                    // padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Color(0xff114488),
                        borderRadius: BorderRadius.circular(10)),
                    child: IconButton(
                      key: ValueKey("camera"),
                      onPressed: () => openFile(1),
                      icon: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.camera,
                            size: 40,
                            color: Colors.white,
                          ),
                          SizedBox(height: 5,),
                          Text("From Camera",style: TextStyle(color: Colors.white),)
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),

          Container(
              width: double.infinity,
              padding: EdgeInsets.symmetric(horizontal: 16),
              margin: EdgeInsets.only(bottom: 10,top: 20),
              child: Text("Histories",textAlign: TextAlign.left,style: TextStyle(fontWeight: FontWeight.w500),)),
          Expanded(
            child: _fileList.isEmpty?Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text("Select a picture to translate",style: TextStyle(fontWeight: FontWeight.w500),),
                    SizedBox(height: 30,),
                  ],
                ),
              ):GridView(
              padding: EdgeInsets.symmetric(horizontal: 16,vertical: 30),
              physics: BouncingScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 25,
                  childAspectRatio: 10/16
              ),
              children: _fileList.map((e) => InkWell(
                onTap: ()=>Navigator.push(context, MaterialPageRoute(builder: (_)=>SharePage(e))),
                child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: Image.file(File(e.path),fit: BoxFit.cover,)
                ),
              )).toList(),
              semanticChildCount: 4,
            ),
          ),
        ],
      ),
    );
  }
}
